<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

use \GatewayWorker\Lib\Gateway;

use Illuminate\Support\Facades\DB;
use Cache;
use Validator;
use Carbon\Carbon;

use  App\Models\ChatMessage;
use  App\Models\WeixinFriendList;
use  App\Models\User;
use  App\Models\PhoneCopy;
class ChatController extends Controller
{
    public function createMessage(Request $request)
    {
        $input = Input::only('type', 'userName','talker','createTime','msgId','ext','isSend','status');
        $validator = Validator::make($input, [        
            'type'      => 'required',
            'userName'         => 'required',
            'talker'     =>  'required',
            'createTime'     => 'required',
            'msgId'     => 'required',
            'ext'     => 'required',
            'isSend'     => 'required',
            'status'     => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        // msg_type
        $data['msg_type'] = $input['type'];
        $data['msg_id'] = $input['msgId'];
        $data['uid'] = $input['userName'];
        $data['to_weixin_uid'] = $input['talker'];
        $data['send_time'] = $input['createTime'];
        $data['ext'] = $input['ext'];
        $data['is_send'] = $input['isSend'];
        $data['status'] = $input['status'];
        $where['msg_id'] = $input['msgId'];
        $where['uid'] = $input['userName'];
        $where['to_weixin_uid'] = $input['talker'];
        $exist = ChatMessage::where($where)->first();
        // WeixinFriendList::where('')
        if($exist){
            $exist->status = $data['status'];
            $exist->save();
            $uid = $data['to_weixin_uid'];
            $this->sendMsgDataToWeb($uid,$data);
        }else{
            $res = ChatMessage::create($data);
            $uid = $data['to_weixin_uid'];
            $this->sendMsgDataToWeb($uid,$data);
        }
        $this->upDateFriend($data['uid'],$data['to_weixin_uid']);
        return $this->returnOk([],'上传成功');
    }

    protected function sendMsgDataToWeb($uid,$data)
    {
        $daata['version'] = 12;
        $daata['imei'] = '123';
        PhoneCopy::create($daata);
        $friend = WeixinFriendList::where('uid',$uid)->first();
        if($friend&&$friend->user_id){
            $user = User::where('id',$friend->user_id)->first();
            if($user&&$user->client_id){
                $new_message = array(
                    'type'=>'NEW_MSG_FROM_PHONE',
                    'data' => $data,
                );
                Gateway::sendToClient($user->client_id, json_encode($new_message));
            }
        }
    }

    protected function upDateFriend($uid,$to_uid)
    {
        $friend = WeixinFriendList::where('uid',$to_uid)->where('weixin_uid',$uid)->first();  
        $friend->today_talked = $friend->today_talked+1;
        $friend->save();
    }
}